<template>
  <div class="edit">
    <h1
      style="
        font-weight: 700;
        font-style: normal;
        font-size: 24px;
        color: #344563;
        margin-bottom: 30px;
      "
    >
      维修质量修改
    </h1>
    <div class="edit-box">
      <div class="form">
        <el-form
          :rules="rules"
          :label-position="labelPosition"
          label-width="80px"
          :model="formLabelAlign"
        >
          <el-form-item label="维修单号">
            <el-input v-model="formLabelAlign.id" disabled> </el-input>
          </el-form-item>
          <el-form-item label="装备名称" prop="equipmentId">
            <el-select
              v-model="formLabelAlign.equipmentId"
              placeholder="装备名称"
              clearable
              style="width: 100%"
            >
              <el-option
                :key="v.equipName"
                v-for="v in dropDown"
                :label="v.equipName"
                :value="v.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="装备编号" prop="equipmentNumber">
            <el-input
              v-model="formLabelAlign.equipmentNumber"
              placeholder="装备编号"
            ></el-input>
          </el-form-item>
          <el-form-item label="检测结果" prop="testResults">
            <el-select
              v-model="formLabelAlign.testResults"
              placeholder="检测结果"
              style="width: 100%"
            >
              <el-option
                :key="v.id"
                v-for="v in result"
                :label="v.statue"
                :value="v.statueId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="form">
        <el-form
          :rules="rules"
          :label-position="labelPosition"
          label-width="80px"
          :model="formLabelAlign"
        >
          <el-form-item label="开始时间" prop="startTime">
            <el-col :span="11" style="width: 100%">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="formLabelAlign.startTime"
                style="width: 100%"
                value-format="yyyy-mm-dd"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <el-col :span="11" style="width: 100%">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="formLabelAlign.endTime"
                style="width: 100%"
                value-format="yyyy-mm-dd"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="检测状态" prop="testStatus">
            <el-select
              v-model="formLabelAlign.testStatus"
              placeholder="检测状态"
              style="width: 100%"
            >
              <el-option
                :key="v.id"
                v-for="v in statue"
                :label="v.statue"
                :value="v.statueId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-button
            style="margin-right: 20px; width: 100px; margin-left: 150px"
            @click="cancel"
            >取消</el-button
          >
          <el-button
            type="danger"
            style="background: #6554c0; border: none; width: 100px"
            @click="Save"
            >保存</el-button
          >
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      labelPosition: "left",
      formLabelAlign: {
        endTime: "",
        equipmentId: "",
        equipmentNumber: "",
        id: "",
        startTime: "",
        testResults: "",
        testStatus: "",
      },
      rules: {
        endTime: [
          { required: true, message: "请输入结束时间", trigger: "blur" },
        ],
        startTime: [
          { required: true, message: "请输入开始时间", trigger: "blur" },
        ],
        testStatus: [
          { required: true, message: "请选择检测状态", trigger: "blur" },
        ],
        testResults: [
          { required: true, message: "请选择检测结果", trigger: "blur" },
        ],
        equipmentId: [
          { required: true, message: "请选择装备", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        equipmentNumber: [
          { required: true, message: "请输入装备ID", trigger: "blur" },
        ],
      },
      //检测结果数据
      result: [],
      //检测状态数据
      statue: [],
      //装备名称数据
      dropDown: [],
      //备份刷新页面防丢失数据
      formData: [],
    };
  },
  methods: {
    //取消返回
    cancel: function () {
      this.$router.push("/home/Wxzl");
    },

    //渲染状态拉框
    getData: function () {
      this.$axios({
        url: "/keepPlan/queryAllStaute",
        method: "post",
      }).then(({ data }) => {
        // this.dropDown = data.equipmentNames;

        this.result.push(data.data[0]);
        this.result.push(data.data[1]);
        this.statue.push(data.data[2]);
        this.statue.push(data.data[3]);
        this.statue.push(data.data[4]);
      });
    },
    //渲染装备下拉框
    getData1: function () {
      this.$axios({
        url: "/equipment/preselect",
        method: "get",
      }).then(({ data }) => {
        this.dropDown = data.equipmentNames;
      });
    },
    //渲染修改界面函数
    getEditT: function (v) {
      this.formLabelAlign.endTime = this.switchDate(v.endTime);
      this.formLabelAlign.startTime = this.switchDate(v.startTime);
      this.formLabelAlign.equipmentNumber = v.equipmentNumber;
      this.formLabelAlign.equipmentId = v.equipment.id;
      this.formLabelAlign.testStatus = v.testStatus;
      this.formLabelAlign.testResults = v.testResults;
      this.formLabelAlign.id = v.id;
    },
    //渲染修改页面
    getEdit: function () {
      let Data = this.$store.state.xjqVuex.quality;
      if (Data == "") {
        this.getEditT(this.formData);
      } else {
        this.getEditT(Data);
      }
    },
    //修改保存
    Save: function () {
      console.log(this.formLabelAlign);
      this.$axios({
        url: "/KeepQuality/update",
        method: "put",
        data: {
          endTime: this.formLabelAlign.endTime,
          equipmentId: this.formLabelAlign.equipmentId,
          equipmentNumber: this.formLabelAlign.equipmentNumber,
          id: this.formLabelAlign.id,
          startTime: this.formLabelAlign.startTime,
          testResults: this.formLabelAlign.testResults,
          testStatus: this.formLabelAlign.testStatus,
        },
      }).then(({ data }) => {
        if (data.code == 0) {
          this.$message.success("修改成功！");
          setTimeout(() => {
            this.$router.push("/home/Wxzl");
          }, 1000);
        } else {
          this.$message.error("修改失败！请重新修改！");
        }
      });
    },
    //时间转换
    switchDate: function (sj) {
      var now = new Date(sj);
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var date = now.getDate();
      return year + "-" + month + "-" + date;
    },
  },
  //挂在后
  mounted: function () {
    this.getData();
    this.getEdit();
    this.getData1();
  },
  created: function () {
    //创建后获取session备份数据
    this.formData = JSON.parse(sessionStorage.getItem("quality"));
  },
};
</script>
<style lang="less" scoped>
.edit {
  padding: 40px;
}
.edit-box {
  display: flex;
  margin-bottom: 30px;
}
.form {
  height: auto;
  flex: 1;
  margin-right: 150px;
}
</style>